package com.fangcang.job.quartz.impl;

import java.util.Random;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.fangcang.job.quartz.QuartzTask;
import com.fangcang.log.imp.QunarTraceLog;
import com.fangcang.log.tracelog.TraceKit;

@Service("quartzTaskImpl")
public class QuartzTaskImpl implements QuartzTask{
	
	private static final Logger logger = Logger.getLogger(QuartzTaskImpl.class);

	@Override
	public void executeTask()  {
		System.out.println("execute QuartzTask QuartzTask");
		logger.trace("execute QuartzTask QuartzTask");
		
		QunarTraceLog startLog = new QunarTraceLog();
		startLog.setAppName("hotel-delivery-server-quartz");
		startLog.setChanelCode("qunar");
		startLog.setBizName("push-change-price");
		startLog.setRemark("日志记录开始");
		
		TraceKit.start(logger, startLog);
		
		String parentId = TraceKit.getTraceId();
		for(int i=0; i<10; i++) {
			QunarTraceLog subLog = new QunarTraceLog();
			subLog.setAppName("hotel-delivery-server-quartz");
			subLog.setBizName("push-change-price");
			subLog.setMerchantCode("M100009" + i);
			subLog.setChanelCode("qunar");
			subLog.setStartTime(System.currentTimeMillis());
			//TraceKit.subStart(logger, subLog, parentId);
			if(i%3==1) {
				subLog.setFailType("rmi_fail");
				subLog.setRemark("服务调用失败");
				try {
					TimeUnit.SECONDS.sleep(new Random(5).nextLong());
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				TraceKit.subError(logger, subLog, parentId);
			}else if(i%3==2){
				subLog.setFailType("error");
				subLog.setRemark("业务处理异常");
				TraceKit.subError(logger, subLog, parentId);
			}else {
				subLog.setRemark("处理成功 ");
				TraceKit.subInfo(logger, subLog, parentId);
			}
			//TraceKit.end(logger, subLog);
		}
		
		startLog.setRemark("日志记录结束");
		TraceKit.end(logger, startLog);
	}

}
